package com.minimalist.common.feign.fallback.lindi;

import com.minimalist.common.feign.client.lindi.LindiDataviewFeignClient;
import com.minimalist.common.feign.config.BaseFallbackFactory;
import com.minimalist.common.response.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.util.List;
import java.util.Map;

/**
 * 林地数据可视化 Feign 客户端降级工厂
 * 使用FallbackFactory可以获取到异常信息，便于问题排查
 *
 * @author 11987
 */
@Component
public class LindiDataviewFeignClientFallbackFactory extends BaseFallbackFactory<LindiDataviewFeignClient> {

    private static final Logger log = LoggerFactory.getLogger(LindiDataviewFeignClientFallbackFactory.class);

    @Override
    public LindiDataviewFeignClient create(Throwable cause) {
        return new LindiDataviewFeignClient() {
            @Override
            public SseEmitter connect(String clientId) {
                logServiceUnavailable("建立SSE连接", clientId, cause);
                // SSE连接失败时返回null，由调用方处理
                return null;
            }

            @Override
            public R<?> startDataPush() {
                logServiceUnavailable("启动数据推送", null, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(),
                        "启动数据推送失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<?> stopDataPush() {
                logServiceUnavailable("停止数据推送", null, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(),
                        "停止数据推送失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<Map<String, Object>> getConnections() {
                logServiceUnavailable("获取当前SSE连接数", null, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(),
                        "获取当前SSE连接数失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<?> pushDataNow() {
                logServiceUnavailable("手动触发数据推送", null, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(),
                        "手动触发数据推送失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<Map<String, Object>> getOverview() {
                logServiceUnavailable("获取平台统计概览", null, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(),
                        "获取平台统计概览失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<List<Map<String, Object>>> getAreaStatistics() {
                logServiceUnavailable("获取各街道镇区林地面积数据", null, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(),
                        "获取各街道镇区林地面积数据失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<List<Map<String, Object>>> getTradeStatistics() {
                logServiceUnavailable("获取各镇区街道林地交易情况", null, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(),
                        "获取各镇区街道林地交易情况失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<List<Map<String, Object>>> getRevenueStatistics() {
                logServiceUnavailable("获取资金营收统计", null, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(),
                        "获取资金营收统计失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<List<Map<String, Object>>> getMapData() {
                logServiceUnavailable("获取地图数据", null, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(),
                        "获取地图数据失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<List<Map<String, Object>>> getSubsidyStatistics() {
                logServiceUnavailable("获取林地补贴申请统计", null, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(),
                        "获取林地补贴申请统计失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<List<Map<String, Object>>> getCompanyStatistics() {
                logServiceUnavailable("获取企业信息注册统计", null, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(),
                        "获取企业信息注册统计失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<Map<String, Object>> getOrderStatistics() {
                logServiceUnavailable("获取交易订单统计", null, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(),
                        "获取交易订单统计失败，服务不可用: " + cause.getMessage());
            }
        };
    }
}